package com.android.lovingpet.community.teams;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseActivity;
import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.base.BaseView;
import com.android.lovingpet.bean.Team;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class TeamsActivity extends BaseActivity<TeamsContract.View, TeamsPresenter> implements TeamsContract.View {

    private List<Team> teams;
    private SmartRefreshLayout refreshLayout;
    private TeamsAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teams);
        ((TextView) findViewById(R.id.tv_title)).setText("认证号");
        RecyclerView rvTeams = findViewById(R.id.rv_teams);
        teams = new ArrayList<>();
        adapter = new TeamsAdapter(teams);
        rvTeams.setLayoutManager(new LinearLayoutManager(this));
        rvTeams.setAdapter(adapter);
        refreshLayout = findViewById(R.id.refresh_layout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getPresenter().getTeams(0);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getPresenter().getTeams(teams.size());
            }
        });
        refreshLayout.autoRefresh();
    }

    @Override
    public TeamsPresenter createPresenter() {
        return new TeamsPresenter();
    }

    @Override
    public TeamsContract.View createView() {
        return this;
    }

    @Override
    public void addTeams(List<Team> teams) {
        this.teams.addAll(teams);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void clear() {
        teams.clear();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void complete() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
            }
        });
    }
}
